package com.software.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SingleTonDemo {

    public static void main(String[] args) {
//        SingleTon singleTon = new SingleTon();
//        SingleTon singleTon1 = new SingleTon();
//        System.out.println(singleTon);
//        System.out.println(singleTon1);
//        System.out.println(singleTon==singleTon1);


//        SingleTon instance = SingleTon.getInstance();
//        SingleTon instance1 = SingleTon.getInstance();
//        System.out.println(instance);
//        System.out.println(instance1);
//        System.out.println(instance == instance1);


//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                SingleTon instance = SingleTon.getInstance();
//                System.out.println(instance);
//            }
//        }).start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                SingleTon instance2 = SingleTon.getInstance();
//                System.out.println(instance2);
//            }
//        }).start();
//    }

        //反射攻击  反射攻击就是指如果拿到字节码文件 就可以利用反射机制 找到构造方法 利用构造方法来生成对象
        try {
            Constructor<SingleTon> declaredConstructor = SingleTon.class.getDeclaredConstructor();
            System.out.println(declaredConstructor.getName());
            declaredConstructor.setAccessible(true);
            //SingleTon singleTon1 = SingleTon.class.newInstance();
            SingleTon singleTon = null;
            try {
                singleTon = declaredConstructor.newInstance(null);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            System.out.println(singleTon);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        SingleTon singleTon2 = SingleTon.getInstance();
        System.out.println(singleTon2);

    }

}
